package it.univaq.di.helpdesk.presentation;

import it.univaq.di.helpdesk.business.common.HelpDeskServiceFactory;
import it.univaq.di.helpdesk.business.common.OfficeService;
import it.univaq.di.helpdesk.business.errors.IntegrityConstraintException;
import it.univaq.di.helpdesk.business.model.Office;
import it.univaq.di.helpdesk.common.utils.AjaxResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class ManageOfficeAction extends MappingDispatchAction {
	
	/* AJAX Request */
	public ActionForward viewofficehistory (ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService  service = factory.getOfficeService();
		
		List<String> buildings = service.findBuildings();
		
		request.setAttribute("buildings", buildings);
		
		return mapping.findForward("admin.viewofficehistory");
	}
	
	/* AJAX Request */
	public ActionForward loadbuildingfloor(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService service = factory.getOfficeService();
		
		List<Integer> floors = service.findBuildingFloor(request.getParameter("building"));
		
		AjaxResponse ajaxResponse = AjaxResponse.getInstance();
		ajaxResponse.sendJson(floors, response.getWriter());
		
		return null;
	}
	
	/* AJAX Request */
	public ActionForward officelist (ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService  service = factory.getOfficeService();
		
		OfficeHistoryForm form = (OfficeHistoryForm) actionForm;
		
		Map<String, String> queryParam = new HashMap<String, String>();
		queryParam.put("building", form.getBuilding());
		queryParam.put("floor", form.getFloor());
		
		List<Office> offices = service.searchForOffices(queryParam);
		
		request.setAttribute("offices", offices);
		return mapping.findForward("admin.officelist"); 
	}
	
	public ActionForward activeaddoffice(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		// Salvataggio del token riutilizzato nel metodo addoffice.
		saveToken(request);
		
		return mapping.findForward("admin.activeaddoffice");
	}
	
	/* AJAX Request */
	public ActionForward addoffice(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		/* controlla se il token inserito in sessione dal metodo activeaddoffice
		 * è valido rispetto a quello inviato con il submit del form e lo elimina 
		 * dopo l'inserimento 
		 */
		if (!isTokenValid(request, false)) {
			return mapping.findForward("common.doublesubmit");
		}	
		
		Office office = new Office();
		OfficeForm officeForm = (OfficeForm)actionForm;
		BeanUtils.copyProperties(office, officeForm);
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService service = factory.getOfficeService();
		
		service.addOffice(office);
		
		// Rimozione del token dalla sessione
		resetToken(request);
		
		return mapping.findForward("common.ok");
	}
	
	public ActionForward activeeditoffice(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		OfficeForm officeForm = (OfficeForm) actionForm;
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService service = factory.getOfficeService();
		
		//recupero l'ID dell'ufficio corrente
		int officeID = Integer.parseInt(request.getParameter("officeID"));
		Office office = service.findOfficeByID(officeID);
		
		//copio il contenuto di user dentro officeForm
		BeanUtils.copyProperties(officeForm, office);
		
		// Salvataggio del token riutilizzato nel metodo editprofile.
		saveToken(request);
		
		return mapping.findForward("admin.activeeditoffice");
	}
	
	/* AJAX Request */
	public ActionForward editoffice(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	
		/* controlla se il token inserito in sessione dal metodo activeaddoffice
		 * è valido rispetto a quello inviato con il submit del form e lo elimina 
		 * dopo l'inserimento 
		 */
		if (!isTokenValid(request, false)) {
			return mapping.findForward("common.doublesubmit");
		}	
		
		Office office = new Office();
		OfficeForm officeForm = (OfficeForm)actionForm;
		BeanUtils.copyProperties(office, officeForm);
		
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService service = factory.getOfficeService();
		
		service.updateOffice(office);
		
		// Rimozione del token dalla sessione
		resetToken(request);
		
		return mapping.findForward("common.ok");
	}
	
	/* AJAX Request */
	public ActionForward deleteoffice(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			
		HelpDeskServiceFactory factory = HelpDeskServiceFactory.getInstance();
		OfficeService service = factory.getOfficeService();
		
		int officeID = Integer.parseInt(request.getParameter("officeID"));
		
		try {
			service.deleteOffice(officeID);
		} catch (IntegrityConstraintException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		}
		
		return null;
	}
}
